/**
 * Copyright 2012 Meruvian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.yama.action;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.meruvian.inca.struts2.rest.ActionResult;
import org.meruvian.inca.struts2.rest.annotation.Action;
import org.meruvian.inca.struts2.rest.annotation.Result;
import org.meruvian.inca.struts2.rest.annotation.Results;
import org.meruvian.inca.struts2.rest.annotation.Action.HttpMethod;
import org.meruvian.inca.struts2.rest.annotation.ActionParam;
import org.meruvian.yama.persistence.EntityListWrapper;
import org.meruvian.yama.persistence.utils.PagingUtils;

import com.example.yama.entity.Category;
import com.example.yama.entity.Item;
import com.example.yama.service.CategoryService;
import com.example.yama.service.ItemService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Dian Aditya
 * 
 */
@Action(name = "/module/item")
@Results({ @Result(name = "list", type = "redirect", location = "/module/item") })
public class ItemAction implements ModelDriven<Object> {
	@ActionParam("item")
	private Item item = new Item();

	@ActionParam("limit")
	private int limit = 10;

	@ActionParam("page")
	private int page = 1;

	@ActionParam("categories")
	private List<Category> categories = new ArrayList<Category>();

	private EntityListWrapper<Item> items = new EntityListWrapper<Item>();

	@Inject
	private ItemService itemService;

	@Inject
	private CategoryService categoryService;

	@Action(name = "/edit/{item.id}", method = HttpMethod.GET)
	public ActionResult edit() {
		item = itemService.findItemById(item.getId());
		categories = categoryService.getCategoriesByName("", 0, 0)
				.getEntityList();

		return new ActionResult("/view/module/item/item-add.ftl")
				.setType("freemarker");
	}

	@Action(name = "/add", method = HttpMethod.GET)
	public ActionResult form() {
		categories = categoryService.getCategoriesByName("", 0, 0)
				.getEntityList();

		return new ActionResult("/view/module/item/item-add.ftl")
				.setType("freemarker");
	}

	@Action(name = "/add", method = HttpMethod.POST)
	public String add() {
		if (item.getId() != null) {
			if (item.getId().trim().equalsIgnoreCase(""))
				item.setId(null);
		}

		if (item.getCategory().getId() != null) {
			if (item.getCategory().getId().trim().equalsIgnoreCase("")) {
				item.setCategory(categoryService.findCategoryById(item
						.getCategory().getId()));
			}
		}

		itemService.saveItem(item);

		return "list";
	}

	@Action(method = HttpMethod.DELETE)
	public String delete() {
		itemService.deleteItem(item);

		return "list";
	}

	@Action
	public ActionResult execute() {
		if (item == null) {
			item = new Item();
		}

		String name = item.getName() == null ? "" : item.getName();

		items = itemService.getItemsByName(name, limit, page - 1);
		items.setTotalPage(PagingUtils.getTotalPage(items.getRowCount(), limit));

		return new ActionResult("/view/module/item/item-list.ftl")
				.setType("freemarker");
	}

	@Action(name = "/{item.id}")
	public ActionResult detail() {
		item = itemService.findItemById(item.getId());

		return new ActionResult("/view/module/item/item-add.ftl")
				.setType("freemarker");
	}

	public Object getModel() {
		return items.getRowCount() < 1 ? item : items;
	}
}
